package com.xin.ssm.service;

import com.xin.ssm.entity.Employee;
import com.xin.ssm.entity.EmployeeExample;
import com.xin.ssm.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    public List<Employee> getAll(Employee employee) {
        String name = employee.getEmpName();
        Integer dId = employee.getdId();

        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();

        if(name.equals(null) || name.equals("")){
            if (dId != null && dId != 0){
                criteria.andDIdEqualTo(dId);
                return employeeMapper.selectByExampleWithDept(example);
            }else {
                return employeeMapper.selectByExampleWithDept(null);
            }
        }else {
            if(dId!=0){
                criteria.andEmpNameEqualTo(name);
                criteria.andDIdEqualTo(dId);
                return employeeMapper.selectByExampleWithDept(example);
            }else{
                criteria.andEmpNameEqualTo(name);
                return employeeMapper.selectByExampleWithDept(example);
            }
        }
    }


    /**
     * 员工保存
     *
     * @param employee
     */
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }


    /**
     * 检验用户名是否可用
     *
     * @param empName
     * @return ture:代表当前姓名可用     false:不可用
     */
    public boolean checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count == 0;
    }

    /**
     * 依照id查员工
     */
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKeyWithDept(id);
        return employee;
    }

    /**
     * 员工更新
     *
     * @param employee
     */
    public void updateEmp(Employee employee) {

        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    /**
     * 员工删除
     *
     * @param id
     */
    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> del_ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria= example.createCriteria();
        criteria.andEmpIdIn(del_ids);
        employeeMapper.deleteByExample(example);
    }
}
